package com.cy.pj.goods.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DefaultGoodsDao implements GoodsDao{
    @Autowired
    private DataSource dataSource;


    @Override
    public List<Map<String, Object>> findGoods() {
        Connection conn=null;
        Statement  stat=null;
        ResultSet    rs=null;
        String sql = "select * from tb_goods";
        try {
            //1.获取jdbc连接
            conn = dataSource.getConnection();
            //2.创建statement对象，基于此对象发送sql
            stat = conn.createStatement();
            //3.发送sql，执行查询
            rs = stat.executeQuery(sql);
            //4.处理结果集(将所有记录封装到List<Map<String, Object>>)
            List<Map<String,Object>> list = new ArrayList<>();
            ResultSetMetaData resultSetMetaData = rs.getMetaData();
            while (rs.next()) {
                //Map<String,Object> map=new HashMap<>();
                /*map.put("id",rs.getInt("id"));
                map.put("name",rs.getString("name"));
                map.put("remark",rs.getString("remark"));
                map.put("createdTime",rs.getTimestamp("createdTime"));
                list.add(map);*/
                list.add(rowMap(resultSetMetaData, rs));
            }
        return list;
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            //5.释放资源
            if(rs!=null)try {
                rs.close();
            }catch (SQLException e){

            }
            if(rs!=null)try {
                stat.close();
            }catch (SQLException e){

            }
            if(rs!=null)try {
                conn.close();
            }catch (SQLException e){

            }

        }

    }
    public  Map<String,Object> rowMap(ResultSetMetaData resultSetMetaData,ResultSet rs) throws SQLException{
        Map<String,Object> map=new HashMap<>();
        int columnCount = resultSetMetaData.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            String columnName = resultSetMetaData.getColumnLabel(i);
            map.put(columnName, rs.getObject(columnName));
        }
        return map;
    }
}

/*
    @Component
public class DefaultGoodsDao implements GoodsDao{
    @Autowired
    private DataSource dataSource;
    List<Map<String,Object>> list;
    Connection conn=null;
    Statement  stat=null;
    ResultSet    rs=null;
    @Override
    public List<Map<String, Object>> findGoods() {

        try {
            //1.获取jdbc连接
            Connection conn = dataSource.getConnection();
            //2.创建statement对象，基于此对象发送sql
            Statement stat = conn.createStatement();
            //3.发送sql，执行查询
            String sql = "select * from dbgoods";
            ResultSet rs = stat.executeQuery(sql);
            //4.处理结果集(将所有记录封装到List<Map<String, Object>>)
            while (rs.next()) {
                Map<String,Object> map=new HashMap<>();
                map.put("id",rs.getInt("id"));
                map.put("name",rs.getString("name"));
                map.put("remark",rs.getString("remark"));
                map.put("createdTime",rs.getTimestamp("createdTime"));
                list.add(map);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            //5.释放资源
            if(rs!=null)try {
                rs.close();
            }catch (SQLException e){

            }
            if(rs!=null)try {
                stat.close();
            }catch (SQLException e){

            }
            if(rs!=null)try {
                conn.close();
            }catch (SQLException e){

            }

        }

    }
}
    private Integer id;
    private String name;
    private String remark;
    private Date createdTime;

    public String getName() {
        return name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public List<Map<String, Object>> findGoods() {
        return null;
    }

    @Override
    public String toString() {
        return "FindGoods{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", remark='" + remark + '\'' +
                ", createdTime=" + createdTime +
                '}';
    }*/
